﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class TeacherInClass : System.Web.UI.Page
    {
        static int classid=0;
        static int teacherid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (classid != 0)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myClass = from c in db.ClassInfo
                                      where c.ClassID == classid
                                      select c.ClassName;
                    foreach (var newclass in myClass)
                    {
                        DropDownField1.Text = newclass.ToString();
                    }
                    var myteahcer = from c in db.TeacherInClass
                                    where c.ClassID == classid
                                    select new { TeacherID = c.TeacherID, TeacherName = c.TeacherInfo.TeacherName, TeacherCode = c.TeacherInfo.TeacherCode, Subject = c.TeacherInfo.Subject };
                    this.Store2.DataSource = myteahcer;
                    this.Store2.DataBind();
                }
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                classid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myClass = from c in db.ClassInfo
                                  where c.ClassID==classid
                                  select c.ClassName;
                foreach (var c in myClass)
                {
                    DropDownField1.Text = c.ToString();
                }
                var myteahcer = from c in db.TeacherInClass
                                where c.ClassID == classid
                                select new { TeacherID = c.TeacherID, TeacherName = c.TeacherInfo.TeacherName, TeacherCode = c.TeacherInfo.TeacherCode, Subject = c.TeacherInfo.Subject };
                this.Store2.DataSource = myteahcer;
                this.Store2.DataBind();
            }
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (classid == 0)
            {
                X.Msg.Alert("提示信息", "请选择一个班级").Show();
            }
            else
            {
                teacherid = 0;
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    teacherid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newteacher = from t in db.TeacherInClass
                                     where t.TeacherID == teacherid && t.ClassID == classid
                                     select t;
                    ExamAnalysis.Model.TeacherInClass newt = newteacher.First();
                    db.TeacherInClass.DeleteOnSubmit(newt);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("TeacherInClass.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个教师").Show();
            }
        }

        //protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        teacherid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myTeacher = from c in db.TeacherInfo
        //                     where c.TeacherID==teacherid
        //                     select c.TeacherName;
        //        foreach (var teacher in myTeacher)
        //        {
        //            DropDownField2.Text = teacher.ToString();
        //        }
        //    }
        //}

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //if (DropDownField2.Text.ToString().Equals("请选择"))
            //{
            //    X.Msg.Alert("提示信息", String.Format("请选择一个教师")).Show();
            //}
            //else
            //{
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                teacherid = Convert.ToInt32(row.RecordID);
            }
                ExamAnalysis.Model.TeacherInClass newtic = new ExamAnalysis.Model.TeacherInClass();
                newtic.ClassID = classid;
                newtic.TeacherID = teacherid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var thetic = from tic in db.TeacherInClass
                                 where tic.TeacherID == teacherid && tic.ClassID == classid
                                 select tic;
                    if (thetic.Count() == 0)
                    {
                        db.TeacherInClass.InsertOnSubmit(newtic);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功添加教师")).Show();
                        teacherid = 0;
                        Response.Redirect("TeacherInClass.aspx");
                    }
                    else
                    {
                        X.Msg.Alert("提示信息", String.Format("该教师已在此班中")).Show();
                    }
                //}

            }
        }
    }
}